﻿using System;
using System.Data;
using System.Xml;
using Gean.Attributes;
using Gean.Collections;
using Pansoft.HIME.Base.Wrapper;

namespace Pansoft.HIME.Base.Entities
{
    /// <summary>业务类型
    /// </summary>
    [Serializable]
    public class BusinessType : IEquatable<BusinessType>
    {
        /// <summary>业务类型
        /// </summary>
        public BusinessType()
        {
            Ranges = new WorkRangeCollection(0);
            Details = new SerializableMap<string, string>(0);
        }

        /// <summary>业务编号
        /// </summary>
        [EntityColumn]
        public string Id { get; set; }

        /// <summary>业务编号
        /// </summary>
        /// <value>
        /// 编号
        /// </value>
        [EntityColumn]
        public string Number { get; set; }

        /// <summary>业务名称
        /// </summary>
        [EntityColumn]
        public string Name { get; set; }

        /// <summary>号票分段编码
        /// </summary>
        [EntityColumn]
        public string CallHead { get; set; }

        /// <summary>所属流程节点的编号
        /// </summary>
        [EntityColumn]
        public string ProgressIndex { get; set; }

        /// <summary>号票起始数字
        /// </summary>
        /// <value>
        /// The ticket number start.
        /// </value>
        [EntityColumn]
        public ushort TicketNumberStart { get; set; }

        /// <summary>号票终止数字
        /// </summary>
        /// <value>
        /// The ticket number end.
        /// </value>
        [EntityColumn]
        public ushort TicketNumberEnd { get; set; }

        /// <summary>对应的多个工作时间段
        /// </summary>
        /// <value>
        /// The ranges.
        /// </value>
        [EntityColumn]
        public WorkRangeCollection Ranges { get; set; }

        /// <summary>描述信息
        /// </summary>
        /// <value>
        /// The infomation.
        /// </value>
        [EntityColumn]
        public string Infomation { get; set; }

        /// <summary>其他信息
        /// </summary>
        /// <value>
        /// The infomation.
        /// </value>
        [EntityColumn]
        public SerializableMap<string, string> Details { get; set; }

        #region IEquatable<BusinessType> Members

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(BusinessType other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.Id, Id);
        }

        #endregion

        #region override

        public object Clone()
        {
            var wintype = new BusinessType
                              {
                                  CallHead = CallHead,
                                  Id = Id,
                                  Infomation = Infomation,
                                  Name = Name,
                                  TicketNumberEnd = TicketNumberEnd,
                                  TicketNumberStart = TicketNumberStart
                              };
            if (Ranges != null)
            {
                foreach (WorkRange range in Ranges)
                    wintype.Ranges.Add(range);
            }
            if (Details != null)
            {
                foreach (var detail in Details)
                    wintype.Details.Add(detail.Key, detail.Value);
            }
            return wintype;
        }

        public override string ToString()
        {
            return string.Format("{0}.{1}", Number, Name);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (BusinessType)) return false;
            return Equals((BusinessType) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (Number.GetHashCode()*397) ^ Id.GetHashCode();
            }
        }

        public static bool operator ==(BusinessType left, BusinessType right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(BusinessType left, BusinessType right)
        {
            return !Equals(left, right);
        }

        #endregion
    }
}